<div class="widget-container">
    <?php
    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
        'id' => 'product-form',
        'htmlOptions' => array(
            'enctype' => 'multipart/form-data',
            'class' => 'form-horizontal'
        ),
    ));
    ?>
    <div class="span9">
        <div class="widget-box">

            <h5 class="title">Chi tiết</h5>
            <div class="widget-content nopadding">
                <div class="control-group">
                    <?php echo $form->labelEx($model, 'name', array('class' => 'control-label')); ?>
                    <div class="controls">
                        <?php
                        echo $form->textField($model, 'name', array('class' => 'span8'));
                        ?>
                        <?php echo $form->error($model, 'name'); ?>
                    </div>
                </div>
                <div class="control-group">
                    <?php echo $form->labelEx($model, 'alias', array('class' => 'control-label')); ?>
                    <div class="controls">
                        <?php
                        echo $form->textField($model, 'alias', array('class' => 'span8'));
                        ?>
                        <?php echo $form->error($model, 'alias'); ?>
                    </div>
                </div>
                <div class="control-group">
                    <?php echo $form->labelEx($model, 'price', array('class' => 'control-label')); ?>
                    <div class="controls">
                        <?php
                        echo $form->textField($model, 'price', array('class' => 'span4'));
                        ?>
                        <span class="resultPrice">
                            <?php
                            if ($model->getScenario() == 'update')
                                echo number_format($model->price, 0, ",", ".") . ' VND'
                                ?>
                        </span>
                        <?php echo $form->error($model, 'price'); ?>
                    </div>
                </div>
                <div class="control-group">
                    <?php echo $form->labelEx($model, 'sale', array('class' => 'control-label')); ?>
                    <div class="controls" id="demo-column">
                        <?php
                        echo $form->textField($model, 'sale', array('class' => 'span4'));
                        ?>
                        <span class="resultSale">
                            <?php
                            if ($model->getScenario() == 'update')
                                echo number_format($model->sale, 0, ",", ".") . ' VND'
                                ?>
                        </span>
                        <?php echo $form->error($model, 'sale'); ?>
                    </div>

                </div>
                <div class="control-group">
                    <?php echo $form->labelEx($model, 'state', array('class' => 'control-label')); ?>
                    <div class="controls">
                        <?php
                        echo $form->textField($model, 'state', array('class' => 'span8'));
                        ?>
                        <?php echo $form->error($model, 'state'); ?>
                    </div>
                </div>
                <div class="control-group">
                    <?php echo $form->labelEx($model, 'image', array('class' => 'control-label')); ?>
                    <div class="controls">
                        <?php echo $form->fileField($model, 'image', array('class' => 'file_upload span6')); ?>
                        <?php echo $form->error($model, 'image'); ?>
                    </div>

                </div>

            </div>
            <div class="control-group">
                <?php echo $form->labelEx($model, 'content', array('class' => 'control-label')); ?>
                <div class="controls">
                    <?php
                    $this->widget('ext.widgets.xheditor.XHeditor', array(
                        'model' => $model,
                        'modelAttribute' => 'content',
                        'config' => array(
                            'id' => 'xheditor_1',
                            'tools' => 'mfull', // mini, simple, mfull, full or from XHeditor::$_tools, tool names are case sensitive
                            'skin' => 'default', // default, nostyle, o2007blue, o2007silver, vista
                            'width' => '100%',
                            'height' => '300px',
//                                    'loadCSS' => XHtml::cssUrl('editor.css'),
                            'upImgUrl' => $this->createUrl('request/uploadFile'),
                            'upImgExt' => 'jpg,jpeg,gif,png',
                        ),
                    ));
                    ?>
                    <?php echo $form->error($model, 'content'); ?>
                </div>
            </div>
            <div class="control-group">
                <?php echo $form->labelEx($model, 'tags', array('class' => 'control-label')); ?>
                <div class="controls">
                    <?php
                    $this->widget('CAutoComplete', array(
                        'model' => $model,
                        'attribute' => 'tags',
                        'url' => array('ajax/suggestTags'),
                        'multiple' => true,
                        'htmlOptions' => array('size' => 50),
                    ));
                    ?>
                    <?php
                    echo $form->error($model, 'tags');
                    ?>
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <?php echo CHtml::submitButton('Đăng tin', array('class' => 'btn btn-success')); ?>
                </div>
            </div>
        </div>
    </div>
    <div class="span3">
        <div class="widget-box">
            <h5 class="title">Danh mục</h5>
            <div class="control-group control-mar">
                <?php
                $pcfk = $this->findCategory($model->category_id);

                if (!empty($category)) {
                    foreach ($category as $key => $value) {
                        ?>
                        <label class="checkbox">
                            <?php
                            if (!empty($pcfk)) {
                                $t = 0;
                                foreach ($pcfk as $v) {
                                    if ($v->id == $value->id) {
                                        $t = 1;
                                    }
                                }
                                if ($t == 0) {
                                    ?> 
                                    <input type="checkbox" name="Product[category_id][]" value="<?php echo $value->id ?>"><?php echo $value->name ?>
                                <?php } else { ?>
                                    <input type="checkbox" name="Product[category_id][]" checked="checked" value="<?php echo $value->id ?>"><?php echo $value->name ?>
                                    <?php
                                }
                            } else {
                                ?>
                                <input type="checkbox" name="Product[category_id][]" value="<?php echo $value->id ?>"><?php echo $value->name ?>
                            <?php } ?>
                        </label>
                        <?php
                        $item = $this->findRoot($value->id);
                        foreach ($item as $val) {
                            if (!empty($pcfk)) {
                                $vi = 0;
                                foreach ($pcfk as $vitem) {
                                    if ($vitem->id == $val->id)
                                        $vi = 1;
                                }
                                if ($vi == 0) {
                                    ?>
                                    <label class="checkbox" style="padding-left: 40px"><input type="checkbox" name="Product[category_id][]" value="<?php echo $val->id ?>"><?php echo $val->name ?></label>
                                <?php } else { ?>
                                    <label class="checkbox" style="padding-left: 40px"><input type="checkbox" checked="checked" name="Product[category_id][]" value="<?php echo $val->id ?>"><?php echo $val->name ?></label>
                                    <?php
                                }
                            } else {
                                ?>
                                <label class="checkbox" style="padding-left: 40px"><input type="checkbox" name="Product[category_id][]" value="<?php echo $val->id ?>"><?php echo $val->name ?></label>
                                <?php
                            }
                        }
                    }
                }
                ?>
                <?php echo $form->error($model, 'category_id'); ?>
            </div>
        </div>
        
    </div>
</div>


<?php
$this->endWidget();
$cs = Yii::app()->getClientScript();
$cs->registerScript('colorboxUpload', '$("a.colorboxUpload").live("click",function(){      
                                                                            $(".colorboxUpload").colorbox({
                                                                                href:"' . Yii::app()->createUrl('admincp/product/image') . '",
                                                                                     opacity: 0,
                                                                                        overlayClose:false,
                                                                                        fixed:true,
                                                                                data:{
                                                                                    "YII_CSRF_TOKEN":"' . Yii::app()->request->csrfToken . '"},
                                                                                title:"",
                                                                                type:"POST",
                                                                                width:"910px", 
                                                                                height:"550px",
                                                                                rel:"nofollow"
                                                                            });
                                                                        });
                                                                            
                                                                ', CClientScript::POS_END
);
?>

</div><!-- form -->

<script>
    $(function() {
        $('#Product_name').keyup(function() {
            title = $('#Product_name').val();
            $('#Product_alias').val(convertToSlug(title));
        });
    });
    $(function() {
        $('#Product_sale').keyup(function() {
            sale = $('#Product_sale').val();
            $('.resultSale').html(FormatToCurrency(sale));
        });
    });
    $(function() {
        $('#Product_price').keyup(function() {
            price = $('#Product_price').val();
            $('.resultPrice').html(FormatToCurrency(price));
        });
    });
</script>